package com.shk.controller;

        import com.aliyuncs.exceptions.ClientException;
        import com.shk.constant.MessageConstant;
        import com.shk.constant.RedisMessageConstant;
        import com.shk.entity.Result;
        import com.shk.utils.SMSUtils;
        import com.shk.utils.ValidateCodeUtils;
        import okhttp3.Request;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidataCodeController {
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/sendCode")
    public Result sendCode(String telephone) {
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        if (code != null) {
            try {
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code.toString());
            } catch (ClientException e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
                /*发送完信息 吧验证码缓存到redis进行短期保存*/
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code.toString());
            /*给前端信息*/
        }  return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }


    @RequestMapping("/send4login")
    public Result send4Login(String telephone){
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        if(code != null){
            try {
                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            } catch (ClientException e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
            }
             jedisPool.getResource().setex("112", 300, code.toString());
            /*String co = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
            System.out.println(co);
*/
        }
        return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
